﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

public partial class BalanceCenter_GoodSales_Stat : PageBase
{
    private static DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            StartTime.Attributes.Add("onclick", "WdatePicker()");
            EndTime.Attributes.Add("onclick", "WdatePicker()");

            BindData();
        }
    }

    #region 窗体设计器生成的代码

    protected override void OnInit(EventArgs e)
    {
        isRequestLogin = true;
        SecondLevelPassword = true;
        RequestLoginPage = "BalanceCenter/GoodSales_Stat.aspx";
        
        base.OnInit(e);
    }

    #endregion

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindData();
    }

    protected void gPager_PageWillChange(object Sender, Shove.Web.UI.PageChangeEventArgs e)
    {
        BindData();
    }

    protected void gPager_SortBefore(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
    {
        BindData();
    }

    protected void g_ItemCreated(object sender, DataGridItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Footer)
        {
            e.Item.SetRenderMethodDelegate(new RenderMethod(DrawGridFooterForG));
        }
    }

    protected void DrawGridFooterForG(HtmlTextWriter output, Control ctl)
    {
        int Amount = 0;             //总销售数量
        double SumPrice = 0;        //销售总额

        if (dt != null && dt.Rows.Count > 0)
        {
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Amount += ShoveEL._Convert.StrToInt(dt.Rows[i]["Amount"].ToString(), 0);
                SumPrice += ShoveEL._Convert.StrToDouble(dt.Rows[i]["SumPrice"].ToString(), 0);
            }

            output.Write("<td align='center'>总计</td>");
            output.Write("<td align='center'></td>");
            //output.Write("<td align='center'></td>");
            //output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");
            output.Write("<td align='center'></td>");  
            output.Write("<td align='center'>" + Amount +"</td>");
            output.Write("<td align='center'>" + SumPrice +"</td>");
        }
    }

    protected void g_ItemDataBound(object sender, DataGridItemEventArgs e)
    {
        //if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.EditItem)
        //{
        //    DataRowView drv = (DataRowView)e.Item.DataItem;
        //    DataRow dr = drv.Row;

        //    string GoodID = dr["GoodID"].ToString();

        //    string StartTime = DateTime.Today.ToString();
        //    string EndTime = DateTime.Today.ToString();

        //    if (this.StartTime.Text != "")
        //    {
        //        StartTime = this.StartTime.Text;
        //    }
        //    if (this.EndTime.Text != "")
        //    {
        //        EndTime = this.EndTime.Text;
        //    }

        //    HyperLink Detail = (HyperLink)e.Item.Cells[8].FindControl("hlDetail");
        //    Detail.NavigateUrl = "GoodSales_Stat2.aspx?GoodID=" + GoodID + "&StartTime=" + StartTime + "&EndTime=" + EndTime;
        //}
    }

    private void BindData()
    {
        DataSet ds = null;

        string GoodName = "";
        string SaleStart = DateTime.Today.ToString();
        string SaleEnd = DateTime.Today.ToString();

        if (tbGoodName.Text.Trim() != "")
        {
            GoodName = tbGoodName.Text.Trim();
        }
        if (this.StartTime.Text != "")
        {
            SaleStart = this.StartTime.Text;
        }
        if (this.EndTime.Text != "")
        {
            SaleEnd = this.EndTime.Text;
        }

        try
        {
            ds = DAL.Procedures.P_GoodSales_Stat.Open(SaleStart, SaleEnd, _User.ID, GoodName);

            dt = ds.Tables[0];

            if (dt != null && dt.Rows.Count >= 0)
            {
                g.DataSource = dt;
                g.DataBind();
                gPager.Visible = (dt.Rows.Count > 0);
            }
        }
        catch { }
    }       

}
